//
//  MainTabBarController.swift
//  mySina
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 tz. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //tabBar是只读属性，可以利用KVC进行赋值
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        //加号按钮的点击事件
        mainTabBar.composeButton.addTarget(self, action: "composeButtonDidClick", forControlEvents: .TouchUpInside)
        //加载UITabBarButton按钮视图
        addChildViewControllers()
    }
    //MARK: 监听加号按钮
    @objc private func composeButtonDidClick() {
//        print(__FUNCTION__)
        let compose = ComposeViewController()
        let nav = UINavigationController(rootViewController: compose)
        //modla 页面
        presentViewController(nav, animated: true, completion: nil)
    }
    private func addChildViewControllers() {
        //首页
        addChildViewController(controller: HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        //发现
        addChildViewController(controller: DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        //消息
        addChildViewController(controller: MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        //我
        addChildViewController(controller: MeTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(controller controller:UIViewController, title:String, imageName:String) {
        let nav = BasNavViewController(rootViewController: controller)
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        addChildViewController(nav)
//        //设置rootViewController
//        let navController = UINavigationController(rootViewController: controller)
//        //设置UITabBarButton的文本
//        controller.title = title  //->使用Controller加载文本可以将navVC上的文字加载出来
//        //设置UItabBarButton的图片
//        navController.tabBarItem.image = UIImage(named: imageName)
//        //添加子视图
//        addChildViewController(navController)
    }
}


